<?php
require 'settings.php';
require 'db.php';

if( isset( $_POST['pricelist_name'] ) )
{
	$pl_name = $_POST['pricelist_name'];
}
?>

<html>
<head>
<title><?php echo $corp_name?> Industry Manager - Materials for pricelist "<?php echo $pl_name ?>" </title>

<style type="text/css">
table, th, td
{
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
}
</style>

</head>
<body>
<h1 class=c_page_title>Materials for pricelist "<?php echo $pl_name ?>"</h1>

<?php
//first we need to get how much of each item we need
$products = array();

$get_name_by_prod_id->bind_param( 'i', $id );
$get_name_by_prod_id->bind_result( $prod_name );

$smf = '[b]Items to produce[/b]'."\n";
$smf .= '[table]'."\n";
$smf .= '[tr]';
$smf .= '[td][center][b]        Name        [/b][/center][/td]';
$smf .= '[td][center][b]        Amount        [/b][/center][/td]';
$smf .= '[/tr]'."\n";

$table_h = '<br /><table><caption>Items to produce</caption><tr>';
$table_h .= '<td style="text-align:center;font-weight:bold">Name</td>';
$table_h .= '<td style="text-align:center;font-weight:bold">Amount</td>';
$table_h .= '</tr>';

$table_f = '</table><br />';

$table_c = '';

foreach( $_POST as $prod_id => $count )
{
	if( preg_match( '/prod_(\d+)/', $prod_id, $matched ) && (0 != $count))
	{
		$products[ $matched[1] ] = $count;
		$id = $matched[1];
		//echo 'Product ID: '. $matched[1].' , count: '.$count.'<br/>';
		$get_name_by_prod_id->execute();
		$get_name_by_prod_id->fetch();
		$table_c .= '<tr>';
		$table_c .= '<td style="text-align:left">'.$prod_name.'</td>';
		$table_c .= '<td style="text-align:right">'.number_format( $count, 0, '.', ',').'</td>';
		$table_c .= '</tr>';

		$smf .= '[tr]';
		$smf .= '[td][left]'.$prod_name.'[/left][/td]';
		$smf .= '[td][right]'.number_format( $count, 0, '.', ',').'[/right][/td]';
		$smf .= '[/tr]'."\n";
	}
}
$get_name_by_prod_id->close();
$smf .= '[/table]'."\n\n";
echo $table_h.$table_c.$table_f;

//now calculate how much do we need of each material
$materials = array();
$pricelist_materials_data_get->bind_param( 'i', $prod_id );
$pricelist_materials_data_get->bind_result( $mat_id, $mat_count );
foreach( $products as $prod_id => $count )
{
	$pricelist_materials_data_get->execute();
	while( $pricelist_materials_data_get->fetch() )
	{
		if( isset( $materials[$mat_id] ) )
		{
			$materials[ $mat_id ] += ($mat_count * $count);
		}
		else
		{
			$materials[ $mat_id ] = ($mat_count * $count);
		}

	}
}
$pricelist_materials_data_get->close();


//we need a list of corp base materials so we can also calculate cost
$corp_base_materials = array();
$show_corp_prices->bind_result( $base_name, $base_typeID, $base_price, $base_volume );
$show_corp_prices->execute();
while( $show_corp_prices->fetch() )
{
	$corp_base_materials[ $base_typeID ] = array('name' => $base_name, 'price' => $base_price, 'volume' => $base_volume);
}
$show_corp_prices->close();

$mat_totals = array();
$totals = array();
$totals[ 'cost' ] = 0;
$totals[ 'volume' ] = 0;
foreach( $materials as $mat_id => $mat_sum )
{
	if( !isset( $mat_totals[$mat_id]['name'] ) )
	{
		$mat_totals[$mat_id]['name'] = $corp_base_materials[$mat_id]['name'];
	}

	if( !isset( $mat_totals[$mat_id]['amount'] ) )
	{
		$mat_totals[$mat_id]['amount'] = $mat_sum;
	}

	//calculate the volume of the material
	$vol = $mat_sum * $corp_base_materials[$mat_id]['volume'];
	if( isset( $mat_totals[$mat_id]['total_vol'] ) )
	{
		$mat_totals[$mat_id]['total_vol'] += $vol;
	}
	else
	{
		$mat_totals[$mat_id]['total_vol'] = $vol;
	}
	$totals[ 'volume' ] += $vol;

	//calculate cost for the material
	$cost = $mat_sum * $corp_base_materials[$mat_id]['price'];
	if( isset( $mat_totals[$mat_id]['cost'] ) )
	{
		$mat_totals[$mat_id]['cost'] += $cost;
	}
	else
	{
		$mat_totals[$mat_id]['cost'] = $cost;
	}
	$totals[ 'cost' ] += $cost;
}

//now we need to print it out
$table_h = '<br /><table><caption>Materials breakdown</caption><tr>';
$table_h .= '<td style="text-align:center;font-weight:bold">Name</td>';
$table_h .= '<td style="text-align:center;font-weight:bold">Amount</td>';
$table_h .= '<td style="text-align:center;font-weight:bold">Volume</td>';
$table_h .= '<td style="text-align:center;font-weight:bold">Cost</td>';
$table_h .= '</tr>';

$table_f = '</table><br />';

$table_c = '';

$smf .= '[b]Materials breakdown[/b]'."\n";
$smf .= '[table]'."\n";
$smf .= '[tr]';
$smf .= '[td][center][b]        Name        [/b][/center][/td]';
$smf .= '[td][center][b]                Amount                [/b][/center][/td]';
$smf .= '[td][center][b]                Volume                [/b][/center][/td]';
$smf .= '[td][center][b]                Cost               [/b][/center][/td]';
$smf .= '[/tr]'."\n";

foreach( $mat_totals as $mat_id => $val )
{
	$table_c .= '<tr>';
	$table_c .= '<td style="text-align:left">'.$val['name'].'</td>';
	$table_c .= '<td style="text-align:right">'.number_format( $val['amount'],2, '.', ',').'</td>';
	$table_c .= '<td style="text-align:right">'.number_format( $val['total_vol'],2, '.', ',').'</td>';
	$table_c .= '<td style="text-align:right">'.number_format( $val['cost'],2, '.', ',').'</td>';
	$table_c .= '</tr>';

	$smf .= '[tr]';
	$smf .= '[td][left]'.$val['name'].'[/left][/td]';
	$smf .= '[td][right]'.number_format( $val['amount'],2, '.', ',').'[/right][/td]';
	$smf .= '[td][right]'.number_format( $val['total_vol'],2, '.', ',').'[/right][/td]';
	$smf .= '[td][right]'.number_format( $val['cost'],2, '.', ',').'[/right][/td]';
	$smf .= '[/tr]'."\n";
}

echo $table_h.$table_c.$table_f;
$smf .= '[/table]'."\n\n";

//now display totals
echo '<h3>';
echo 'Total cost of materials: '.number_format( $totals['cost'],2, '.', ',').' ISK<br />';
echo 'Total volume of materials: '.number_format( $totals['volume'],2, '.', ',').'m<sup>3</sup><br />';
echo '</h3>';

$smf .= '[b]'."\n";
$smf .= 'Total cost of materials: '.number_format( $totals['cost'],2, '.', ',').' ISK'."\n";
$smf .= 'Total volume of materials: '.number_format( $totals['volume'],2, '.', ',').'m[sup]3[/sup]'."\n";
$smf .= '[/b]'."\n";
?>

SMF-compatible report (copy-paste);
<pre style="border:1px solid black">
<?php echo $smf ?>
</pre>

<br />
<a href="view_pricelists.php">View pricelists</a><br />
<a href="index.php">Back to main screen</a>
</body>
</html>